/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.packager.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import sshtunneler.tools.Tool;
import sshtunneler.tools.ToolsManager;

public class InstalledTools {

	public static InstalledTools inst = new InstalledTools();
	private List<Tool> tool_index = new ArrayList<Tool>();
	private HashMap<Tool, Boolean> tools = new HashMap<Tool, Boolean>();

	private InstalledTools() {
	}

	public int nb_tools() {
		assert this.tool_index.size() == this.tools.size();
		return this.tools.size();
	}

	public void refresh() {
		HashMap<Tool, Boolean> new_tools = new HashMap<Tool, Boolean>();
		List<Tool> new_index = new ArrayList<Tool>();

		for (Tool t : ToolsManager.inst.all_tools()) {
			new_tools.put(t, false);
			new_index.add(t);
		}

		for (Tool t : this.tool_index) {
			if (ToolsManager.inst.manages(t)) {
				new_tools.put(t, this.tools.get(t));
				new_index.add(t);
			}
		}
		this.tools = new_tools;
		this.tool_index = new_index;
	}

	public boolean tool_known(Tool tool) {
		for (Tool t : this.tool_index) {
			if (t.getClass().equals(tool.getClass())) {
				return true;
			}
		}
		return false;
	}

	public void add_tool(Tool tool) {
		this.tool_index.add(tool);
		this.tools.put(tool, false);
	}

	public void set_tool(Tool tool, Boolean installed) {
		this.tools.put(tool, installed);
	}

	public Tool get_tool(int index) {
		return this.tool_index.get(index);
	}

	public boolean tool_installed(Tool tool) {
		for (Tool t : this.tools.keySet()) {
			if (t.getClass().equals(tool.getClass())) {
				return this.tools.get(t);
			}
		}
		return false;
	}
}
